# !/bin/bash
# description: auto-install-docker, check jdk1.8,redis5.0.8,zookeeper,MongoDB
#

#./test.sh -a 1 -b 2 -c 3
#while getopts ":host:username:password:src_file:dest_file:run" opt
#do
#    case $opt in
#        host)
#        echo "参数a的值$OPTARG"
#        ;;
#        username)
#        echo "参数b的值$OPTARG"
#        ;;
#        password)
#        echo "参数c的值$OPTARG"
#        ;;
#        src_file)
#        echo "参数a的值$OPTARG"
#        ;;
#        dest_file)
#        echo "参数b的值$OPTARG"
#        ;;
#        run)
#        echo "参数c的值$OPTARG"
#        ;;
#        ?)
#        echo "未知参数"
#        exit 1;;
#    esac
#done

#当前机器IP
HOST=$(ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
#脚本所在的目录
CURRPATH=$(cd $(dirname $0); pwd)
CFGPATH=$CURRPATH/conf

function createDir(){
    #从键盘读取用户输入
	read path
	if [ ! $path ]; then
		path = '/home/admin/conf'
		return $($path)
	fi
	if [ ! -d $path  ]; then
		mkdir $path
		if [ $? -eq  0 ]; then
			#红色字体
			echo -e "\n\033[31m [WARNING]##### $path新建失败，请检查路径是否合法######## \033[0m"
			$0 mkdir			
		else
			#绿色字体
			echo -e "\n\033[32m [OK]##### $path新建完成######## \033[0m"
			return $($path)
		fi
	else
		#绿色字体
		echo -e "\n\033[32m [OK]##### $path已经存在######## \033[0m"
		return $($path)
    fi
}


function checkEnv(){
	echo -e "\n\033[1;41;33m当前机器IP为：{$HOST} \033[0m"
	
    #检测java
	javaStatus=`whereis java | wc -l`
	if [ $javaStatus -eq  0  ]; then
		#红色字体
		echo -e "\n\033[31m [WARNING]##### java服务没有启动 ######## \033[0m" 
    else
	    if [ "$1" == "-y" ]; then
		    #java -version 2>&1 | sed '1!d' | sed -e 's/"//g' | awk '{print $3}'
			javaState=`java -version 2>&1 | sed '1!d' | sed -e 's/"//g' -e 's/version//'`
			#绿色字体
			echo -e "\n\033[32m [OK]##### java服务正在运行,版本如下：########  \n$javaState \033[0m" 
		else
			#绿色字体
			echo -e "\n\033[32m [OK]##### javar服务正在运行######## \033[0m"
		fi
    fi
	
    #检测docker
	dockerStatus=`whereis docker | wc -l`
	if [ $dockerStatus -eq  0  ]; then
		#红色字体
		echo -e "\n\033[31m [WARNING]##### docker服务没有启动 ######## \033[0m" 
    else
	    if [ "$1" == "-y" ]; then
			dockerState=`docker version --format '{{.Server.Version}}'`
			#绿色字体
			echo -e "\n\033[32m [OK]##### docker服务正在运行,版本信息如下：########  \n$dockerState \033[0m" 
		else
			#绿色字体
			echo -e "\n\033[32m [OK]##### docker服务正在运行######## \033[0m"
		fi
    fi
	
	#检测zookeeper
	zkStatus=`ps aux|grep zookeeper | grep -v grep  | wc -l`
	if [ $zkStatus -eq  0  ]; then
		#红色字体
		echo -e "\n\033[31m [WARNING]##### zookeeper服务没有启动 ######## \033[0m" 
    else
	    if [ "$1" == "-y" ]; then
			zkState=`ps aux|grep zookeeper | grep -v grep`
			#绿色字体
			echo -e "\n\033[32m [OK]##### zookeeper服务正在运行,详情如下：########  \n$zkState \033[0m" 
		else
			#绿色字体
			echo -e "\n\033[32m [OK]##### zookeeper服务正在运行######## \033[0m"
		fi
    fi
	
	
    #检测redis
	redisStatus=`ps aux|grep redis | grep -v grep | wc -l`
	if [ $redisStatus -eq  0  ]; then
		#红色字体
		echo -e "\n\033[31m [WARNING]##### redis服务没有启动 ######## \033[0m" 
    else
		if [ "$1" == "-y" ]; then
			redisState=`ps aux|grep redis | grep -v grep`
			#绿色字体
			echo -e "\n\033[32m [OK]##### redis服务正在运行,详情如下：########  \n$redisState \033[0m" 
		else
			#绿色字体
			echo -e "\n\033[32m [OK]##### redis服务正在运行######## \033[0m"
		fi
	    
    fi
	#检测mysql
	mysqlStatus=`ps aux|grep mysqld | grep -v grep | wc -l`
	if [ $mysqlStatus -eq  0 ]; then
		#红色字体
		echo -e "\n\033[31m [WARNING]##### mysql服务没有启动 ######## \033[0m" 
    else
		if [ "$1" == "-y" ]; then
			mysqlState=`ps aux|grep mysqld | grep -v grep`
			#绿色字体
			echo -e "\n\033[32m [OK]##### mysql服务正在运行,详情如下：########  \n$mysqlState \033[0m"
		else
			#绿色字体
			echo -e "\n\033[32m [OK]##### mysql服务正在运行########\033[0m"
		fi
	    
    fi
	
	#检测mongo
	mongoStatus=`ps aux | grep mongo | grep -v grep | wc -l`
	if [ $mongoStatus -eq  0 ]; then
		#红色字体
		echo -e "\n\033[31m [WARNING]##### mongo服务没有启动 ######## \033[0m" 
    else
		if [ "$1" =="-y" ]; then
			mongoState=`ps aux | grep mongo | grep -v grep`
			#绿色字体
			echo -e "\n\033[32m [OK]##### mongo服务正在运行,详情如下：########  \n$mongoState \033[0m"
		else
			#绿色字体
			echo -e "\n\033[32m [OK]##### mongo服务正在运行 ######## \033[0m"
		fi
    fi
}



#执行入口
checkEnv $1

